---
title: "R Notebook"
output: html_notebook
---

This is an [R Markdown](http://rmarkdown.rstudio.com) Notebook. When you execute code within the notebook, the results appear beneath the code. 

Try executing this chunk by clicking the *Run* button within the chunk or by placing your cursor inside it and pressing *Ctrl+Shift+Enter*. 

```{r}
library(neuralnet)
```

Add a new chunk by clicking the *Insert Chunk* button on the toolbar or by pressing *Ctrl+Alt+I*.

When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the *Preview* button or press *Ctrl+Shift+K* to preview the HTML file).

```{r}
dataPath<-"data/"
dat <- read.csv(paste0(dataPath,"test_sample.csv"))
```

```{r}
#dat<-as.data.frame(dat)
#dat$class<-as.factor(dat$class)
dat
```
```{r}
plot(dat$x,dat$y,col="orange",pch=16,ylim=c(0,1),xlim=c(0,1))
points(dat$x[dat$class==1],dat$y[dat$class==1],col="blue",pch=16)
```
```{r}
set.seed(1)
m1 <- neuralnet(class~x+y,data=dat,hidden=3,err.fct="ce",linear.output=FALSE,threshold=0.1,stepmax = 1e6)
plot(m1)
```
```{r}
predict <- (compute(m1,dat[c("x","y")])$net.result > 0.5)*1
head(predict)
head(dat)
```


```{r}
#(conf <- table(pred = predict[,2], true = dat[,"class"]))
(conf <- table(pred = predict, true = dat[,"class"]))
```


```{r}
(accuracy = (conf[1,1]+conf[2,2])/sum(conf))
```
```{r}
ctrl <- trainControl(method = "cv", number = 5)
set.seed(1)
nnGTrain <- train(class~x+y, data = dat, method = "nnet", 
                  trControl = ctrl, 
                  tuneGrid = expand.grid(.size=3:8,.decay=.3),
                  maxit = 1000, trace = FALSE, metric="Accuracy")
print(nnGTrain)
```
```{r}
plot(nnGTrain)
```

